import { ApiProperty } from '@nestjs/swagger'
import { BaseEntity, Column, CreateDateColumn, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'

import { UserEntity } from '~/modules/user/user.entity'

@Entity({ name: 'health_user_alarm' })
@Index(['type', 'startAt', 'endAt'], { unique: true })
export class UserAlarmEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ name: 'user_id', nullable: false, comment: '用户ID' })
  @ApiProperty({ description: '用户ID' })
  userId: number

  @Column({ nullable: true, comment: '设备ID' })
  @ApiProperty({ description: '设备ID' })
  uid: string

  @Column({ nullable: true, comment: '告警类型' })
  @ApiProperty({ description: '告警类型' })
  type: number

  @Column({ nullable: true, comment: '告警内容' })
  @ApiProperty({ description: '告警内容' })
  content: string

  @Column({ nullable: true, comment: '状态', default: 0 })
  @ApiProperty({ description: '状态' })
  status: number

  @CreateDateColumn({ name: 'start_at' })
  @ApiProperty({ description: '开始时间' })
  startAt: Date

  @Column({ name: 'end_at', nullable: true })
  @ApiProperty({ description: '结束时间' })
  endAt: Date

  @ManyToOne(() => UserEntity, user => user.id, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user: UserEntity

  // @ManyToOne(() => DeviceEntity, device => device.uid)
  // @JoinColumn({ name: 'uid', referencedColumnName: 'uid' })
  // device: DeviceEntity
}
